package Parser;
/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */

public class LexBuffer {


    StringBuffer data;
    int buffer;
    int pos;

    String token;


    /*
      Tags:
        table
        row
        col

        img
        div
        p
        br
     */

    public LexBuffer(StringBuffer data) {
        this.data = data;
        buffer = -1;
        pos = 0;
    }

    // Leaves the body tag in the token
    public void gotoBody() {
        if (token == null)
          nextToken();

        while (token != null && !(token.length() >= 5) &&
               !token.substring(0, 5).toLowerCase().equals("<body"))
            nextToken();
    }

    public String getToken() {
        return (token);
    }

    public boolean hasToken()
    {
        return (token != null);
    }

    public int read()
    {
        int datum;

        if (pos >= data.length())
          return -1;

        datum = data.charAt(pos);
        pos++;

        return datum;
    }

    public void nextToken() {
        char cur;

        if (buffer == -1) {
            buffer = read();
        }

        // EOF
        if (buffer == -1) {
            token = null;
            return;
        }


        cur = (char) buffer;

        // Trim any white space
        while (isWhiteSpace(cur))
        {
            buffer = read();
            // EOF
            if (buffer == -1) {
                token = null;
                return;
            }
            cur = (char) buffer;
        }

        if (cur == '<') {
            // Is it a comment?
           /* if ( ((char) buffer) == '!') {
              token = "";
              while ( !(cur == '>' && token.length() > 2 && token.charAt(token.length() - 1) == '-' && token.charAt(token.length() - 2) == '-') && buffer != -1) {
		token += cur;
		buffer = read();
                cur = (char) buffer;
              }
              token += cur;
              buffer = read();
              cur = (char) buffer;
            } else */{
              // Tag start, read to closing
              token = "";
              while (cur != '>' && buffer != -1) {
		token += cur;
		buffer = read();
                cur = (char) buffer;
              }
              token += cur;
              buffer = read();
              cur = (char) buffer;
            }
        } else {
            token = "";
            // Not a tag, read until a tag or white space is encountered and call this a text block
            while (cur != '<' && !isWhiteSpace(cur) && !isLineFeed(cur) && buffer != -1) {
                token += cur;
                buffer = read();
                cur = (char) buffer;
            }
	}
    }

    public boolean isWhiteSpace(char c)
    {
        return Character.isWhitespace(c);
    }

    public boolean isLineFeed(char c) {
	if (c == '\r' || c == '\n')
	    return true;
	return false;
    }
}
